package com.ctvit.sjk.knowledge.app.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.ctvit.sjk.knowledge.app.pojo.domain.TabAppApiKey;
import com.ctvit.sjk.knowledge.app.pojo.dto.AppApiKeyDTO;
import com.ctvit.sjk.knowledge.app.pojo.vo.AppApiKeyGetVO;
import com.ctvit.sjk.knowledge.app.service.ITabAppApiKeyService;
import com.ctvit.sjk.knowledge.core.controller.KnowledgeController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 应用API密钥Controller
 *
 * @author heyingcheng
 * @date 2025/3/22
 */
@RestController
@RequestMapping("knowledge/application")
public class TabAppApiKeyController extends KnowledgeController {

    private final ITabAppApiKeyService tabAppApiKeyService;

    public TabAppApiKeyController(ITabAppApiKeyService tabAppApiKeyService) {
        this.tabAppApiKeyService = tabAppApiKeyService;
    }

    /**
     * 查询应用API密钥列表
     */
    @GetMapping("{appId}/api_key")
    public AjaxResult list(@PathVariable String appId) {
        LambdaQueryWrapper<TabAppApiKey> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(TabAppApiKey::getUpdateTime);
        return success(tabAppApiKeyService.list(wrapper).stream().map(AppApiKeyGetVO::new).collect(Collectors.toList()));
    }

    /**
     * 创建应用API密钥
     */
    @PostMapping("{appId}/api_key")
    public AjaxResult create(@PathVariable String appId) {
        String apiKeyId = IdWorker.getIdStr();

        TabAppApiKey entity = new TabAppApiKey();
        entity.setApiKeyId(apiKeyId);
        entity.setAppId(appId);
        entity.setSecretKey(IdWorker.get32UUID());
        entity.setCrossDomainList(JSON.toJSONString(new JSONArray()));
        entity.setActiveFlag(true);
        entity.setCreateId(CREATE_ID);
        entity.setCreateTime(LocalDateTime.now());
        entity.setUpdateId(CREATE_ID);
        entity.setUpdateTime(LocalDateTime.now());
        tabAppApiKeyService.save(entity);

        return success(new AppApiKeyGetVO(tabAppApiKeyService.getById(apiKeyId)));
    }

    @PutMapping("{appId}/api_key/{apiKeyId}")
    public AjaxResult update(@PathVariable String appId, @PathVariable String apiKeyId, @RequestBody AppApiKeyDTO dto) {
        LambdaUpdateWrapper<TabAppApiKey> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Objects.nonNull(dto.getIsActive()), TabAppApiKey::getActiveFlag, dto.getIsActive());
        wrapper.set(Objects.nonNull(dto.getAllowCrossDomain()), TabAppApiKey::getAllowCrossDomain, dto.getAllowCrossDomain());
        wrapper.set(Objects.nonNull(dto.getCrossDomainList()), TabAppApiKey::getCrossDomainList, JSON.toJSONString(dto.getCrossDomainList()));
        wrapper.set(TabAppApiKey::getUpdateId, CREATE_ID);
        wrapper.set(TabAppApiKey::getUpdateTime, LocalDateTime.now());
        wrapper.eq(TabAppApiKey::getAppId, appId);
        wrapper.eq(TabAppApiKey::getApiKeyId, apiKeyId);
        tabAppApiKeyService.update(wrapper);

        return success(new AppApiKeyGetVO(tabAppApiKeyService.getById(apiKeyId)));
    }

    @DeleteMapping("{appId}/api_key/{apiKeyId}")
    public AjaxResult update(@PathVariable String appId, @PathVariable String apiKeyId) {
        LambdaUpdateWrapper<TabAppApiKey> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(TabAppApiKey::getAppId, appId);
        wrapper.eq(TabAppApiKey::getApiKeyId, apiKeyId);
        return success(tabAppApiKeyService.remove(wrapper));
    }

}
